<?php
//$Id: Timer.php 40 2009-04-02 14:56:47Z vipzhicheng $

class Cos_Toolbox_Timer
{
    const ID = '$Id: Timer.php 40 2009-04-02 14:56:47Z vipzhicheng $';
    
    const TIMER_RUN = 1;
    const TIMER_PAUSE = 2;
    const TIMER_STOP = 4;

    private static $_timer = array();

    private function __construct() {}

    public static function start($timerName)
    {
        if (!array_key_exists($timerName, self::$_timer)) {
            self::$_timer[$timerName]['startTime'] = microtime(true);
            self::$_timer[$timerName]['pauseTime'] = '';
            self::$_timer[$timerName]['lastPauseTime'] = '';
            self::$_timer[$timerName]['stopTime'] = '';
            self::$_timer[$timerName]['state'] = self::TIMER_RUN;
        } else {
            throw new Exception("timer exist!");
        }
    }

    public static function pause($timerName)
    {
        if (array_key_exists($timerName, self::$_timer)) {
            if (self::$_timer[$timerName]['state'] == self::TIMER_PAUSE) {
                self::$_timer[$timerName]['pauseTime'] += microtime(true) - self::$_timer[$timerName]['lastPauseTime'];
                self::$_timer[$timerName]['lastPauseTime'] = null;
                self::$_timer[$timerName]['state'] = self::TIMER_RUN;
                return self::TIMER_RUN;
            } else {
                self::$_timer[$timerName]['lastPauseTime'] = microtime(true);
                self::$_timer[$timerName]['state'] = self::TIMER_PAUSE;
                return self::TIMER_PAUSE;
            }
        } else {
            throw new Exception("timer not exist!");
        }

    }

    public static function stopAll()
    {
        foreach (self::$_timer as $timerName => $timerData) {
            self::stop($timerName);
        }
    }

    public static function stop($timerName)
    {
        if (array_key_exists($timerName, self::$_timer)) {
            if (self::$_timer[$timerName]['state'] == self::TIMER_STOP) {
                throw new Exception("timer has been stoped!");
            } else {
                self::$_timer[$timerName]['stopTime'] = microtime(true);
                self::$_timer[$timerName]['lastPauseTime'] = null;
                self::$_timer[$timerName]['state'] = self::TIMER_STOP;
            }
        } else {
            throw new Exception("timer not exist!");
        }
    }

    public static function reset($timerName)
    {
        if (array_key_exists(self::$_timer[$timerName])) {
            unset(self::$_timer[$timerName]);
        } else {
            throw new Exception("timer not exist!");
        }
    }

    public static function show($timerName)
    {
        if (array_key_exists($timerName, self::$_timer)) {
            if (self::$_timer[$timerName]['state'] == self::TIMER_STOP) {
                return self::$_timer[$timerName]['stopTime'] - self::$_timer[$timerName]['startTime'] - self::$_timer[$timerName]['pauseTime'];
            } elseif (self::$_timer[$timerName]['state'] == self::TIMER_PAUSE) {
                return self::$_timer[$timerName]['lastPauseTime'] - self::$_timer[$timerName]['startTime'] - self::$_timer[$timerName]['pauseTime'];
            } else {
                return microtime(true) - self::$_timer[$timerName]['startTime'] - self::$_timer[$timerName]['pauseTime'];
            }
        } else {
            throw new Exception("timer not exist!");
        }
    }

    public static function getAllTimerData()
    {
        return self::$_timer;
    }
}
